package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要新增书的名字");
        String name = scanner.nextLine();
        System.out.println("请输入新增书的作者");
        String author = scanner.nextLine();
        System.out.println("请输入新增书的价格");
        int price = scanner.nextInt();
        System.out.println("请输入新增书的类型");
        String type = scanner.nextLine();
        //构造书的对象
        Book book = new Book(name,author,price,type);
        //记录当前书的 数量，用于计算
        int currentSize = bookList.getUsedSize();
        //从i下标开始查找是否有同类书籍
        for (int i = 0; i < currentSize; i++) {
            Book tmp = bookList.getBooks(i);
            if(tmp.getName().equals(book.getName())) {
                System.out.println("存在这本书，无法重复添加!");
                return;
            }
        }
        //没有重复的书,开始新增
        bookList.setBooks(book,currentSize);
        bookList.setUsedSize(currentSize+1);
    }
}
